﻿//this class use ace library timer_queue to implement timerimpl interface
//andycai_sc 2011-05-28

#ifndef ACETIMERIMPL_H
#define ACETIMERIMPL_H

#include <ace/Timer_Queue.h>
#include <ace/Thread_Mutex.h>
#include "GParallel/GTimerImpl.h"

class ACETimerImpl : public GTimerImpl
{
public:
	ACETimerImpl(GTimer* timer);
	virtual ~ACETimerImpl();

	void beginImpl(long millisc);
	void cancelImpl();

private:
	struct ACETimerRefCounter
	{
		ACETimerRefCounter();
		~ACETimerRefCounter();
		void addReference();
		long decReference();
		ACE_Timer_Queue* m_timer;
		ACE_Thread_Mutex m_refMutex; 
		unsigned long m_refCount;
	};

	static ACETimerRefCounter*& getTimerRef();
	static void freeTimerRef(ACETimerRefCounter*& timerRef);
	long m_timerId;

	ACETimerRefCounter*& m_aceTimerRef;

	class TimerEventHandler : public ACE_Event_Handler
	{
	public:
		TimerEventHandler();
		virtual ~TimerEventHandler();

		int handle_timeout(const ACE_Time_Value &, const void *);
	};
	TimerEventHandler m_timerEventHandler;
};

#endif
